<template>
  <el-dialog
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    destroy-on-close
    :title="title"
    :visible.sync="dialogVisible"
    width="1300px"
    custom-class="crud-dialog"
    :before-close="handleClose"
    append-to-body
    center
  >
    <div class="area-content">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称：" :prop="`name`">
              <el-input v-model="form.name"> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型：" :prop="`type`">
              <el-select v-model="form.type" placeholder="请选择类型">
                <el-option v-for="item in areaTypeList" :label="item.label" :value="item.value" :key="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注：" prop="remark">
              <el-input type="textarea" v-model="form.remark"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <div>区域边界：</div>
            <div style="width: 100%; height: 400px">
              <DrawPolygon ref="drawPolygonRef" :shapeWkt="form.geom" />
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleOk">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addArea, editArea } from '@/api/channelArea'
import DrawPolygon from '@/components/map/drawPolygon'
export default {
  components: {
    DrawPolygon
  },
  props: {
    areaTypeList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },

  data() {
    return {
      dialogVisible: false,
      loading: false,
      title: '',
      form: {},
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
        ],
        type: [{ required: true, message: '请选择类型', trigger: 'change' }]
      },
      type: ''
    }
  },
  mounted() {},
  methods: {
    async show(row) {
      this.dialogVisible = true
      if (!row) {
        this.title = '新增区域'
      } else {
        this.title = '修改区域'
        this.form = { id: row.id, name: row.name, type: row.type, remark: row.remark, geom: row.geom }
      }
    },

    handleOk() {
      console.log('🤡 ~ this.form:', this.form)
      this.$refs['formRef'].validate(async (valid) => {
        if (valid) {
          this.loading = true

          const polygonWkt = this.$refs.drawPolygonRef.getPolygon()
          if (!polygonWkt) {
            this.$message.warning('请绘制区域边界')
            return
          }
          let data = { ...this.form, geom: polygonWkt }
          if (data.id) {
            await editArea(data)
            this.$message.success(`更新告警模型成功`)
          } else {
            await addArea(data)
            this.$message.success(`添加告警模型成功`)
          }
          this.loading = false
          this.handleClose()
          this.$emit('handleRefresh')
        } else {
          return false
        }
      })
    },

    handleClose() {
      this.dialogVisible = false
      this.form = {}
    }
  }
}
</script>
<style scoped lang="scss">
@import '@/assets/styles/variables';
::v-deep .el-dialog__body {
  max-height: 700px !important;
  overflow-y: scroll;
  @include scrollBar();
}
.area-content {
  ::v-deep .el-select {
    width: 100% !important;
  }
  .select-style1 {
    ::v-deep .el-select {
      width: 42% !important;
    }
  }
}
</style>
